/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.util;

import java.util.Collection;

/**
 * PaginatedResult Object, this object represents a single paginatedResult of a result set.
 * @author Adam
 * @version $Id: PaginatedResult.java 11 2009-03-27 06:33:32Z a.ruggles $
 * 
 * Created on Feb 18, 2009 at 10:24:36 PM 
 * @param <T> The class type of objects contained in the paginatedResult of results.
 */
public class PaginatedResult<T> {
	/**
	 * The current page number.
	 */
	private final int page;

	/**
	 * A collection of results for one paginatedResult.
	 */
	private final Collection<T> results;

	/**
	 * The number of results per page.
	 */
	private final int resultsPerPage;

	/**
	 * The total number of pages.
	 */
	private final int totalPages;

	/**
	 * The total number of results represented by the entire result set.
	 */
	private final int totalResults;

	/**
	 * Constructs a paginatedResult of results.
	 * @param results A collection of results for the paginatedResult.
	 * @param totalResults The total number of results represented by the entire result set.
	 */
	public PaginatedResult(final Collection<T> results, final int totalResults, final int page,
			final int resultsPerPage) {
		this.results = results;
		this.totalResults = totalResults;
		this.page = page;
		this.resultsPerPage = resultsPerPage;
		if (this.resultsPerPage == 0) {
			this.totalPages = (int) Math.ceil((this.totalResults / 1d));
		} else {
			this.totalPages = (int) Math.ceil((this.totalResults / this.resultsPerPage));
		}
	}

	/**
	 * Returns page.
	 * @return the page.
	 */
	public int getPage() {
		return page;
	}

	/**
	 * Returns results.
	 * @return the results.
	 */
	public Collection<T> getResults() {
		return results;
	}

	/**
	 * Returns resultsPerPage.
	 * @return the resultsPerPage.
	 */
	public int getResultsPerPage() {
		return resultsPerPage;
	}

	/**
	 * Returns totalPages.
	 * @return the totalPages.
	 */
	public int getTotalPages() {
		return totalPages;
	}

	/**
	 * Returns totalResults.
	 * @return the totalResults.
	 */
	public int getTotalResults() {
		return totalResults;
	}
}
